from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis

rsp = ImageSynthesis.call(
    model=ImageSynthesis.Models.wanx_v1,  # 模型名称
    prompt='生成一张河边的小船，船上有个小女孩，动漫风格，参考游戏《王者荣耀》的画风', # 提示语
    n=4,  # 图片数量
    size='720*1280', # 图片尺寸
    api_key='sk-c98c8f9c9e474aea9c4d56bd52fcc001',
)

if rsp.status_code == HTTPStatus.OK:
    for result in rsp.output.results:
        file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
        with open('./%s' % file_name, 'wb+') as f:
            f.write(requests.get(result.url).content)

else:
    print("不OK")
